<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代理商id" prop="agentId">
        <el-input
          v-model="queryParams.agentId"
          placeholder="请输入代理商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代理商名称" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:statement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          v-if="isShow"
          @click="setRatio"
        >设置比例</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:statement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:statement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:statement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="statementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="代理商id" align="center" prop="agentId" />
      <el-table-column label="代理商名称" align="center" prop="agentName" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="收益金额" align="center" prop="incomeAmount" />
      <el-table-column label="曝光量" align="center" prop="amountOfExposure" />
      <el-table-column label="点击量" align="center" prop="clickVolume" />
      <el-table-column label="点击率" align="center" prop="clickRate" />
      <el-table-column label="ecmp" align="center" prop="ecmp" />
      <el-table-column label="录入时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="ecpc" align="center" prop="ecpc" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:statement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:statement:remove']"
          >删除</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            v-if="isShow"-->
<!--            @click="setRowRatio(scope.row)"-->
<!--          >设置比例</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收益管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="代理商">
          <el-select v-model="form.agent" placeholder="请选择代理商">
            <el-option
              v-for="item in agentOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId+','+item.nickName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="类型" />
        </el-form-item>
        <el-form-item label="收益金额" prop="incomeAmount">
          <el-input v-model="form.incomeAmount" placeholder="请输入收益金额" />
        </el-form-item>
        <el-form-item label="曝光量" prop="amountOfExposure">
          <el-input v-model="form.amountOfExposure" placeholder="请输入曝光量" />
        </el-form-item>
        <el-form-item label="点击量" prop="clickVolume">
          <el-input v-model="form.clickVolume" placeholder="请输入点击量" />
        </el-form-item>
        <el-form-item label="点击率" prop="clickRate">
          <el-input v-model="form.clickRate" placeholder="请输入点击率" />
        </el-form-item>
        <el-form-item label="ecmp" prop="ecmp">
          <el-input v-model="form.ecmp" placeholder="请输入ecmp" />
        </el-form-item>
<!--        <el-form-item label="ecpc" prop="ecpc">-->
<!--          <el-input v-model="form.ecpc" placeholder="请输入ecpc" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="ratioOpenTitle" :visible.sync="ratioOpen" width="500px" append-to-body>
      <el-form ref="ratioForm" :model="ratioForm"  label-width="80px">
        <el-form-item label="收益比例" prop="yieldRate">
          <el-input v-model="ratioForm.yieldRate" placeholder="请输入收益比例" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRatioForm">确 定</el-button>
        <el-button @click="cancelRatioForm">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="rowTitle" :visible.sync="rowOpen" width="500px" append-to-body>
      <el-form ref="rowForm" :model="rowForm"  label-width="80px">
        <el-form-item label="收益比例" prop="yieldRate">
          <el-input v-model="rowForm.managerRate" placeholder="请输入收益比例" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRowForm">确 定</el-button>
        <el-button @click="cancelRowForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import { listStatement, getStatement, delStatement, addStatement, updateStatement,getUserList,getPermission,setRatio,getRatio,getRowRatio,setRowRatio } from "@/api/system/statement";
import {isAdmin} from "@/api/system";

export default {
  name: "Statement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      isShow: false,
      ratioOpen: false,
      ratioOpenTitle: "",
      ratioForm: {},
      rowOpen: false,
      rowTitle: "",
      rowForm: {},
      isAdmin:false,
      // 总条数
      total: 0,
      // 收益管理表格数据
      statementList: [],
      // 角色选项
      agentOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: null,
        agentName: null,
        type: null,
        incomeAmount: null,
        amountOfExposure: null,
        clickVolume: null,
        clickRate: null,
        ecmp: null,
        ecpc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.isAdminUser();
    this.getPermission();
  },
  methods: {
    /** 查询收益管理列表 */
    getList() {
      this.loading = true;
      listStatement(this.queryParams).then(response => {
        this.statementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    isAdminUser() {
      isAdmin().then(response => {
        if(response.data === "1"){
          this.isAdmin = true;
        }
      });
    },
    getPermission() {
      getPermission().then(response => {
        if(response.data == '1'){
          this.isShow = true;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },    // 取消按钮
    cancelRatioForm() {
      this.ratioOpen = false;
      this.reset();
    },
    cancelRowForm() {
      this.rowOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agentId: null,
        agentName: null,
        type: null,
        incomeAmount: null,
        amountOfExposure: null,
        clickVolume: null,
        clickRate: null,
        ecmp: null,
        ecpc: null,
        agentOptions:[]
      };
      this.ratioForm ={
        yieldRate: null
      };
      this.rowForm ={
        id: null,
        managerRate: null
      };
      this.resetForm("form");
      this.resetForm("ratioForm");
      this.resetForm("rowForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    setRatio() {
      getRatio().then(response => {
        this.ratioOpen = true;
        this.ratioOpenTitle = "设置收益比例";
        const ratioForm = {};
        ratioForm.yieldRate = response.data;
        this.ratioForm = ratioForm;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getUserList().then(response => {
        this.agentOptions = response.data;
        this.open = true;
        this.title = "添加收益管理";
      })

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStatement(id).then(response => {
        getUserList().then(userList => {
          this.agentOptions = userList.data;
          this.form = response.data;
          this.form.agent = response.data.agentId+","+response.data.agentName;
          this.open = true;
          this.title = "修改收益管理";
        })
      });
    },
    /** 修改按钮操作 */
    setRowRatio(row) {
      this.reset();
      const id = row.id || this.ids
      getRowRatio(id).then(response => {
        this.rowOpen = true;
        this.rowTitle = "设置收益比例";
        this.rowForm.managerRate = response.data;
        this.rowForm.id = id;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let agent = this.form.agent.split(",");
          this.form.agentId = agent[0];
          this.form.agentName = agent[1];
          if (this.form.id != null) {
            updateStatement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitRatioForm() {
      this.$refs["ratioForm"].validate(valid => {
        setRatio(this.ratioForm.yieldRate).then(response => {
          if(response.code == 200){
            this.$modal.msgSuccess("设置成功");
            this.ratioOpen = false;
          }
        });
      });
    },
    /** 提交按钮 */
    submitRowForm() {
      this.$refs["rowForm"].validate(valid => {
        setRowRatio(this.rowForm).then(response => {
          if(response.code == 200){
            this.$modal.msgSuccess("设置成功");
            this.rowOpen = false;
          }
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收益管理编号为"' + ids + '"的数据项？').then(function() {
        return delStatement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/statement/export', {
        ...this.queryParams
      }, `statement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
